package com.jsyh.maidu.presenter;

import android.content.Context;

import com.jsyh.maidu.config.ConfigValue;
import com.jsyh.maidu.config.SPConfig;
import com.jsyh.maidu.http.BaseDelegate;
import com.jsyh.maidu.http.OkHttpClientManager;
import com.jsyh.maidu.model.BaseModel;
import com.jsyh.maidu.model.Collection;
import com.jsyh.maidu.model.ShopInfoModel;
import com.jsyh.maidu.utils.SPUtils;
import com.jsyh.maidu.views.CollectionView;
import com.jsyh.maidu.views.ShopInfoView;
import com.squareup.okhttp.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZHY_9 on 2017/7/11.
 */

public class CollectionPresenter extends BasePresenter {

    private CollectionView collectionView;

    private ShopInfoView shopInfoView;

    public CollectionPresenter(CollectionView collectionView) {
        this.collectionView = collectionView;
    }

    public CollectionPresenter(ShopInfoView shopInfoView) {
        this.shopInfoView = shopInfoView;
    }

    public void initCollectData(Context context) {
        initLoadDialog(context);
        mLoadingDialog.show();
        OkHttpClientManager.getAsyn(context, ConfigValue.APP_IP + "stores/get_stores",
                new BaseDelegate.ResultCallback<Collection>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {
                mLoadingDialog.dismiss();
            }

            @Override
            public void onResponse(Collection response, Object tag) {
                mLoadingDialog.dismiss();
                collectionView.getCollectData(response);
            }
        });

    }

    public void getShopInfo (Context context, String suppid) {
        Map<String, String> params = new HashMap<>();
        params.put("suppid", suppid);
        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "stores/get_suppinfo", params, new BaseDelegate.ResultCallback<ShopInfoModel>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {

            }

            @Override
            public void onResponse(ShopInfoModel response, Object tag) {
                shopInfoView.getShopInfo(response);
            }
        });

    }

    public void collectShop (Context context, String suppid){
        initLoadDialog(context);
        mLoadingDialog.show();
        Map<String, String> param = new HashMap<>();
        param.put("key", (String) SPUtils.get(context, SPConfig.KEY, ""));
        param.put("suppid", suppid);
        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "follow_shop/guanzhu_supplier", param, new BaseDelegate.ResultCallback<BaseModel>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {
                mLoadingDialog.dismiss();
            }

            @Override
            public void onResponse(BaseModel response, Object tag) {
                mLoadingDialog.dismiss();
                shopInfoView.getCollectShopResult(response);
            }
        });
    }
}
